package com.tigercel.xiangchengyun.ui.fragments

import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.jcodecraeer.xrecyclerview.XRecyclerView
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.adapter.HotVideoAdapter
import com.tigercel.xiangchengyun.adapter.NewsAdapter
import com.tigercel.xiangchengyun.adapter.NewsBannerAdapter
import com.tigercel.xiangchengyun.entity.Banner
import com.tigercel.xiangchengyun.entity.HotVideoEntity
import com.tigercel.xiangchengyun.ui.activities.MoreActivity
import com.tigercel.xiangchengyun.ui.activities.VideoAndWebActivity
import com.tigercel.xiangchengyun.ui.custom.lbanners.LMBanners
import com.tigercel.xiangchengyun.ui.custom.lbanners.transformer.TransitionEffect
import com.tigercel.xiangchengyun.utils.ExpandGridView
import com.tigercel.xiangchengyun.utils.Global
import com.tigercel.xiangchengyun.utils.JsonUtil
import com.tigercel.xiangchengyun.utils.PreferencesUtil
import kotlinx.android.synthetic.main.fragment_video_normal.*
import org.jetbrains.anko.support.v4.toast
import java.text.SimpleDateFormat

class VideoNormalFragment : Fragment() {
    companion object {

        fun newInstance(channelPosition: Int): VideoNormalFragment? {
            val fragment = VideoNormalFragment()
            val bundle = Bundle()
            bundle.putInt("channelPosition", channelPosition)
            fragment.arguments = bundle
            return fragment

        }

    }

    private var containerView: View? = null
    private var banner: LMBanners<Any>? = null
    private var headBanner: View? = null
    private var channelPosition: Int = 0
    private var hotVideo: View? = null
    private var tvvideo: View? = null
    private var festivalvideo: View? = null
    private var name1: TextView? = null
    private var name2: TextView? = null
    private var name3: TextView? = null
    private var more1: LinearLayout? = null
    private var more2: LinearLayout? = null
    private var more3: LinearLayout? = null
    private var grid1: ExpandGridView? = null
    private var grid2: ExpandGridView? = null
    private var grid3: ExpandGridView? = null
    private var hotVideoList: MutableList<HotVideoEntity> = ArrayList()
    private var tvvideoList: MutableList<HotVideoEntity> = ArrayList()
    private var festivalvideoList: MutableList<HotVideoEntity> = ArrayList()
    private var hotVideoAdapter: HotVideoAdapter? = null
    private var hotVideoAdapter1: HotVideoAdapter? = null
    private var hotVideoAdapter2: HotVideoAdapter? = null
    private var localDatas: PreferencesUtil?=null
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        containerView = inflater.inflate(R.layout.fragment_video_normal, container, false)
        if (arguments != null) {
            channelPosition = arguments!!.getInt("channelPosition")
        }
        return containerView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        recyclerview.layoutManager = LinearLayoutManager(activity)
        recyclerview.adapter = NewsAdapter(ArrayList()) {}
        hotVideo = View.inflate(activity, R.layout.hot_video, null)
        tvvideo = View.inflate(activity, R.layout.hot_video, null)
        festivalvideo = View.inflate(activity, R.layout.hot_video, null)
        headBanner = View.inflate(activity, R.layout.banners, null)
        grid1 = hotVideo?.findViewById(R.id.hot_video_recycleview)
        grid2 = tvvideo?.findViewById(R.id.hot_video_recycleview)
        grid3 = festivalvideo?.findViewById(R.id.hot_video_recycleview)
        name1 = hotVideo?.findViewById(R.id.video_title)
        name2 = tvvideo?.findViewById(R.id.video_title)
        name3 = festivalvideo?.findViewById(R.id.video_title)
        more1 = hotVideo?.findViewById(R.id.li_more)
        more2 = tvvideo?.findViewById(R.id.li_more)
        more3 = festivalvideo?.findViewById(R.id.li_more)
        banner = headBanner?.findViewById(R.id.banners)
        banner!!.setIndicatorBottomPadding(8)
        banner!!.setHoriZontalTransitionEffect(TransitionEffect.Default)
        localDatas= PreferencesUtil(activity)
        getContent("create")
        recyclerview.addHeaderView(hotVideo)
        recyclerview.addHeaderView(tvvideo)
        recyclerview.addHeaderView(festivalvideo)
        recyclerview.refresh()
        more1!!.setOnClickListener(View.OnClickListener {
            var intent = Intent()
            intent.putExtra("title", name1!!.text)
            intent.setClass(activity, MoreActivity::class.java)
            startActivity(intent)
        })
        more2!!.setOnClickListener(View.OnClickListener {
            var intent = Intent()
            intent.putExtra("title", name2!!.text)
            intent.setClass(activity, MoreActivity::class.java)
            startActivity(intent)
        })
        more3!!.setOnClickListener(View.OnClickListener {
            var intent = Intent()
            intent.putExtra("title", name3!!.text)
            intent.setClass(activity, MoreActivity::class.java)
            startActivity(intent)
        })
    }

    override fun onDestroy() {
        super.onDestroy()
//        banner!!.clearImageTimerTask()
    }

    override fun onResume() {
        super.onResume()

    }
    private fun getContent(type: String) {
        hotVideoList.clear()
        tvvideoList.clear()
        festivalvideoList.clear()
        val devicesId = Settings.System.getString(activity!!.getContentResolver(), Settings.System.ANDROID_ID)
        val sdf = SimpleDateFormat("yyyy-MM-dd")
        OkGo.get<String>(Global.baseUrl + "v1/channel/content")
                .tag(this)
                .params("channel_id", channelPosition)
                .params("device_code", devicesId)
                .params("user_id", Global.commonUserId)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        val content = response!!.body()
                        val status = JsonUtil.get_key_string("status", content!!)
                        val msg = JsonUtil.get_key_string("msg", content!!)
                        val code = JsonUtil.get_key_string("code", content!!)
                        val data = JsonUtil.get_key_string("data", content!!)
                        if (status == "0") {
                            var banners1: MutableList<Banner>
                            banners1 = ArrayList()
                            banners1.clear()
                            val banners = JsonUtil.getList("banner", data)
                            if (banners.isNotEmpty()) {
                                if (type == "create") {
                                    recyclerview.addHeaderView(headBanner)
                                }
                                for (i in 0 until banners.size) {
                                    var img = JsonUtil.get_key_string("img", banners[i])
                                    var url = JsonUtil.get_key_string("url", banners[i])
                                    var name = JsonUtil.get_key_string("title", banners[i])
                                    var bannerBean = Banner(i, name, img, url)
                                    banners1.add(bannerBean)
                                }
                                banner!!.setAdapter(NewsBannerAdapter(activity), banners1 as List<Any>?)
                            }
                            name1!!.text = "火热快视频"
                            name2!!.text = "电视栏目"
                            name3!!.text = "百姓春晚"
                            val hot_quick = JsonUtil.getList("hot_quick", data)
                            for (i in 0 until hot_quick.size) {
                                var content_id = JsonUtil.get_key_string("content_id", hot_quick[i])
                                var title = JsonUtil.get_key_string("title", hot_quick[i])
                                var thumb_img = JsonUtil.get_key_string("thumb_img", hot_quick[i])
                                var url = JsonUtil.get_key_string("url", hot_quick[i])
                                var pub_time = JsonUtil.get_key_string("pub_time", hot_quick[i]).toLong()
                                var hotVideoEntity = HotVideoEntity(content_id, title, thumb_img, url, pub_time)
                                hotVideoList.add(hotVideoEntity)
                            }
                            hotVideoAdapter = HotVideoAdapter(activity!!.applicationContext, hotVideoList)
                            grid1!!.adapter = hotVideoAdapter
                            grid1!!.setOnItemClickListener { parent, view, position, id ->
                                val intent = Intent()
                                intent.putExtra("videourl", hotVideoList.get(position).url)
                                intent.putExtra("eweburl", "")
                                intent.setClass(activity, VideoAndWebActivity::class.java)
                                startActivity(intent)

                            }
                            val tv = JsonUtil.getList("tv", data)
                            for (i in 0 until tv.size) {
                                var content_id = JsonUtil.get_key_string("content_id", tv[i])
                                var title = JsonUtil.get_key_string("title", tv[i])
                                var thumb_img = JsonUtil.get_key_string("thumb_img", tv[i])
                                var url = JsonUtil.get_key_string("url", tv[i])
                                var pub_time = JsonUtil.get_key_string("pub_time", tv[i]).toLong()
                                var hotVideoEntity = HotVideoEntity(content_id, title, thumb_img, url, pub_time)
                                tvvideoList.add(hotVideoEntity)
                            }
                            hotVideoAdapter1 = HotVideoAdapter(Global.context, tvvideoList)
                            grid2!!.adapter = hotVideoAdapter1
                            val festival = JsonUtil.getList("festival", data)
                            for (i in 0 until festival.size) {
                                var content_id = JsonUtil.get_key_string("content_id", festival[i])
                                var title = JsonUtil.get_key_string("title", festival[i])
                                var thumb_img = JsonUtil.get_key_string("thumb_img", festival[i])
                                var url = JsonUtil.get_key_string("url", festival[i])
                                var pub_time = JsonUtil.get_key_string("pub_time", festival[i]).toLong()
                                var hotVideoEntity = HotVideoEntity(content_id, title, thumb_img, url, pub_time)
                                festivalvideoList.add(hotVideoEntity)
                            }
                            hotVideoAdapter2 = HotVideoAdapter(activity!!.applicationContext, festivalvideoList)
                            grid3!!.adapter = hotVideoAdapter2
                            recyclerview.refreshComplete()
                            recyclerview.setLoadingListener(object : XRecyclerView.LoadingListener {
                                override fun onRefresh() {
                                    getContent("refresh")
                                }

                                override fun onLoadMore() {
//                                    getMore()
                                }
                            })
                        } else {
                            toast(getString(R.string.readFaile) )
                        }
                    }

                })
    }
}
